package com.insightfullogic.java8.examples.chapter7;

import com.insightfullogic.java8.examples.chapter1.Album;
import com.insightfullogic.java8.examples.chapter1.Artist;
import com.insightfullogic.java8.examples.chapter1.SampleData;
import com.insightfullogic.java8.examples.chapter1.Track;

import java.util.ArrayList;
import java.util.List;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lili
 * @description ${DESCRIPTION}
 * @create 2018-01-20 下午1:10
 * @since
 **/
public class OrderRefactor {

    public List<Artist> artists = SampleData.getThreeArtists();
    public static List<Album> albums = new ArrayList<>();


    public long countRunningTime() {
        long count = 0;
        for (Album album : albums) {
            for (Track track : album.getTrackList()) {
                count += track.getLength();
            }
        }
        return count;
    }

    public long countMusicians() {
        long count = 0;
        for (Album album : albums) {
            count += album.getMusicianList().size();
        }
        return count;
    }

    public long countTracks() {
        long count = 0;
        for (Album album : albums) {
            count += album.getTrackList().size();
        }
        return count;
    }

    public long countRunningTime2() {
        return albums.stream().mapToLong(album -> album.getTracks().mapToLong(track -> track.getLength()).sum()).sum();
    }

    public long countMusicians2() {
        return albums.stream().mapToLong(album -> album.getMusicians().count()).sum();
    }

    public long countTracks2() {
        return albums.stream().mapToLong(album -> album.getTracks().count()).sum();
    }

    public long countFeature(ToLongFunction<Album> function) {
        return albums.stream().mapToLong(function).sum();
    }

    public long countTracks3() {
        return countFeature(album -> album.getTracks().count());
    }

    public long countRunningTime3() {
        return countFeature(album -> album.getTracks()
                .mapToLong(track -> track.getLength())
                .sum());
    }

    public long countMusicians3() {
        return countFeature(album -> album.getMusicians().count());
    }

    public static void main(String[] args) {

        String collect = Stream.of("sssssxx", "sss", "ddSCSsdc", "ujhbgvf", "cecfv", "btAX")
                .filter(e -> e.length() > 3)
                .peek(e -> System.out.println("filter value is" + e +"   "))
                .map(String::toUpperCase)
                .peek(e -> System.out.println("map value is " + e))
                .collect(Collectors.joining("-", "{", "}"));
        System.out.println(collect);

        List<String> collect1 = Stream.of("one", "two", "three", "four")
                .filter(e -> e.length() > 3)
                .peek(e -> System.out.println("Filtered value: " + e))
                .map(String::toUpperCase)
                .peek(e -> System.out.println("Mapped value: " + e))
                .collect(Collectors.toList());
        System.out.println(collect1);

//        <R> Stream<R> map(Function<? super T, ? extends R> mapper);  理解map  消费t，而生产r   pecs


    }


}
